package com.sanyang.pda.ui.movement;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sanyang.pda.adapter.MovementAdapter;
import com.sanyang.pda.bean.MoveRespose;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.contract.MovementContract;
import com.sanyang.pda.presenter.MovementPresenter;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MovementFragment extends BaseMvpFragment<MovementPresenter> implements MovementContract.View {
    @BindView(R.id.tv_now_location)
    TextView tvNowLocation;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.scan)
    ScanButView scan;
    @BindView(R.id.bt_putaway)
    Button btPutaway;
    MovementAdapter adapter;
    private boolean isIn=false;
    @Override
    public MovementPresenter getPresenter() {
        return new MovementPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_movement;
    }

    @Override
    public void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter=new MovementAdapter(R.layout.item_movement,new ArrayList<>());
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case 1://库位
                        tvNowLocation.setText(scanBean.getCode());
                        break;
                    case 2://作业库位
                        tvNowLocation.setText(scanBean.getCode());
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        presenter.mobileInventoryInfo(scanBean.getCode());
                        break;
                }
            }
        });
    }

    @Override
    public void getData() {

    }

    @OnClick(R.id.bt_putaway)
    public void onViewClicked() {
        if (TextUtils.isEmpty(tvNowLocation.getText())){
            showMessage(getString(R.string.storage_location)+getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvTray.getText())){
            showMessage(getString(R.string.tray)+getString(R.string.not_be_empty));
            return;
        }
        if (isIn){
            presenter.moveIn(tvTray.getText().toString(),tvNowLocation.getText().toString());
        }else {
            presenter.moveOut(tvTray.getText().toString(),tvNowLocation.getText().toString());
        }
    }

    @Override
    public void setData(MoveRespose moveRespose) {
        if (moveRespose.getButton() == 0){
            isIn=false;
            btPutaway.setText(getString(R.string.moveout));
        }else {
            isIn=true;
            btPutaway.setText(getString(R.string.movein));
        }
        adapter.setNewInstance(moveRespose.getWarehouseStockArticleVoList());
    }

    @Override
    public void onSuccess() {
        tvTray.setText("");
        tvNowLocation.setText("");
        adapter.setNewInstance(new ArrayList<>());
    }
}
